﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using MvcApp.CommonSupport;
using MvcApp.CommonSupport.Filter;
using DoNet.Common.Helper;
using DoNet.FrameWork.Repository.Specification;
using Authority.Entity;
using Authority.Services;

namespace MVC.Controllers
{
    public class HomeController : Controller
    {
        private readonly IUserService _userService;

        public HomeController(IUserService userservice)
        {
            _userService = userservice;
        }

        public ActionResult LogOn()
        {
            return View();
        }

        //[AuthorizeFilter(ModuleName = "001", PermisionType = 1)]
        //[Authorize]
        public ActionResult Index()
        {
            CurrentUser currentUser = new CurrentUser();
            ViewBag.CurrentUser = currentUser;
            return View();
        }

        [HttpPost]
        public JsonResult LogOnByAndPassword(string userid, string password, string code)
        {
            if (this.Session["ValidateCode"] == null || code != this.Session["ValidateCode"].ToString())
            {
                return Json(new { IsSuccess = false, Message = "验证码错误，请重新输入" });
            }

            User entity = this._userService.CheckUserAndPassword(userid, password);
            if (entity == null)
            {
                return Json(new { IsSuccess = false, Message = "用户名或密码错误" });
            }

            if (!entity.IsEnabled)
            {
                return Json(new { IsSuccess = false, Message = "您的账号已被禁用，请联系管理员" });
            }

            FormsAuthentication.SetAuthCookie(entity.UserId.ToString(), false);
            return Json(new { IsSuccess = true, Message = "登陆成功" });
        }

        public ActionResult LogOut()
        {
            FormsAuthentication.SignOut();
            return RedirectToAction("LogOn");
        }

        public ActionResult ChangedPassword(string password, string oldPassword)
        {
            User entity = _userService.CheckUserAndPassword(this.User.Identity.Name, oldPassword);
            if (entity == null)
            {
                return Json(new { IsSuccess = false, Message = "密码错误" },
                    "text/x-json", JsonRequestBehavior.AllowGet);
            }
            _userService.ChangedPassword(entity, password);
            return Json(new { IsSuccess = true, Message = "修改成功" },
                                "text/x-json", JsonRequestBehavior.AllowGet);
        }
        

        //验证码
        public ActionResult ValidateCode()
        {
            ValidateGraphicHelper graphic = new ValidateGraphicHelper();
            string code = graphic.CreateValidateNumber(4);
            this.Session["ValidateCode"] = code;
            return File(graphic.CreateValidateGraphic(code), "image/jpeg");
        }

        #region 返回值测试

        public ActionResult ReturnStr()
        {
            return Content("{'Name':'chen'}");
        }

        public ActionResult ReturnJson()
        {
            var complexObject = new
            {
                CustomerId = Guid.NewGuid(),
                Boss = "Zhen.Chen",
                OrderList = new List<Order> {
                    new Order { ID=Guid.NewGuid(), Name="Soft"},
                    new Order { ID=Guid.NewGuid(), Name="Fruit"},
                    new Order { ID=Guid.NewGuid(), Name="Bouns"},
                    new Order { ID=Guid.NewGuid(), Name="Brief"},
                    new Order { ID=Guid.NewGuid(), Name="Market"}
                }
            };

            return Json(complexObject, JsonRequestBehavior.AllowGet);
        }
        #endregion
    }

    class Order
    {
        public Guid ID { get; set; }

        public string Name { get; set; }
    }
}
